<template>
	<view class="mine-container" :style="{height: `${windowHeight}px`}">
		<!--顶部个人信息栏-->
		<view class="header-section">
			<view class="flex padding justify-between">
				<view class="flex align-center">
					<view v-if="!avatar" class="cu-avatar xl round bg-white">
						<view class="iconfont icon-people text-gray icon"></view>
					</view>
					<image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round"
						mode="widthFix" style="width: 200rpx;height: 200rpx;">
					</image>
					<view v-if="!name" @click="handleToLogin" class="login-tip">
						点击登录
					</view>
					<view v-if="name" @click="handleToInfo" class="user-info">
						<view class="u_title">
							{{ name }}
						</view>
						<view class="u_title">
							{{user.dept.deptName}}

						</view>
						<view class="u_title">
							{{user.phonenumber }}
						</view>
					</view>
				</view>
				<!-- <view @click="handleToInfo" class="flex align-center">
					<text>个人信息</text>
					<view class="iconfont icon-right"></view>
				</view> -->
			</view>
		</view>

		<view class="content-section">
			<view class="mine-action" style="display: flex;justify-content: space-evenly;">
				<view class="mine-actions grid col-4 text-center">
					<view style="font-size: 38rpx;width: 100%;">已领总数</view>
					<view style="font-size: 36rpx;width: 100%;margin-top: 20rpx;">{{yi}}</view>
				</view>
				<view class="mine-actions grid col-4 text-center">
					<view style="font-size: 38rpx;width: 100%;">本月已领总数</view>
					<view style="font-size: 36rpx;width: 100%;margin-top: 20rpx;">{{dai}}</view>
				</view>
			</view>

			<view class=" menu-list">
				<view class="list-cell list-cell-arrow" @click="handleToEditInfo">
					<view class="menu-item-box">
						<view class="iconfont icon-user menu-icon"></view>
						<view>编辑资料</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleHelp">
					<view class="menu-item-box">
						<view class="iconfont icon-edit menu-icon"></view>
						<view>领用记录</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" style="display: flex;justify-content: space-between;"
					@click="handleAbout">
					<view class="menu-item-box">
						<view class="iconfont icon-community menu-icon"></view>
						<view>系统消息</view>
					</view>
					<view v-if="this.reads>0" class="hong">
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleFeed">
					<view class="menu-item-box">
						<view class="iconfont icon-friendfill menu-icon"></view>
						<view>意见反馈</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleToSetting">
					<view class="menu-item-box">
						<view class="iconfont icon-password menu-icon"></view>
						<view>修改密码</view>
					</view>
				</view>
				<view class="list-cell list-cell-arrow" @click="handleLogout">
					<view class="menu-item-box">
						<view class="iconfont icon-logout menu-icon"></view>
						<view>退出登录</view>
					</view>
				</view>

			</view>

		</view>
	</view>
</template>

<script>
import {getUserProfile} from "@/api/system/user";
import {listUsage} from "@/api/usage/usage";
import {listNews} from "@/api/system/news";

export default {
		data() {
			return {
				reads: "",
				items: [],
				user: {
					deptId: undefined,
					userName: undefined,
					phonenumber: undefined,
					dept: Object,
				},
				usageList: [],
				usage: [],
				news: [],
				yi: "",
				dai: "",
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 999999999,
					usageName: null,
					usagePeople: null,
					usageDate: null,
					usageDepartment: null,
					usageState: null,

				},
				name: this.$store.state.user.name,
				version: getApp().globalData.config.appInfo.version,
			};
		},
		created() {

			this.getUser();
			this.getUsage();
			this.getList();

		},
		onLoad() {
			this.footMsgFun() //调用底部方法
		},
		onShow() {
			this.footMsgFun() //调用底部方法
		},
		computed: {
			avatar() {
				return this.$store.state.user.avatar
			},

			windowHeight() {
				return uni.getSystemInfoSync().windowHeight - 50
			},

		},
		methods: {
			getUser() {
				const me = this;
				getUserProfile().then(response => {
					me.user = response.data
				})
			},
			getUsage() {
				listUsage(this.queryParams).then(Response => {
					this.usageList = Response.rows;
					this.usage = this.usageList.filter(item => item.usagePeople === this.user.userName);
					let totalItems = this.usage.reduce((acc, pro) => acc + pro.usageQuantity, 0);
					this.yi = totalItems;


					const currentDate = new Date();
					const currentMonth = currentDate.getMonth(); // 注意月份是从0开始计数的
					const currentYear = currentDate.getFullYear();

					// 过滤本月的数据
					this.news = this.usage.filter(item => {
						// 假设item.usageDate是一个日期字符串，如"2023-07-01T00:00:00"
						const usageDate = new Date(item.usageDate);
						console.log(usageDate)
						return usageDate.getMonth() === currentMonth && usageDate.getFullYear() ===
							currentYear;
					});

					// 计算本月的领取总数
					let totalItem = this.news.reduce((acc, pro) => acc + pro.usageQuantity, 0);
					this.dai = totalItem;
				})

			},
			handleToInfo() {
				this.$tab.navigateTo('/pages/mine/info/index')
			},
			handleToEditInfo() {
				this.$tab.navigateTo('/pages/mine/info/edit')
			},
			handleToSetting() {
				this.$tab.navigateTo('/pages/mine/pwd/index')
			},
			handleToLogin() {
				this.$tab.reLaunch('/pages/login')
			},
			handleToAvatar() {
				this.$tab.navigateTo('/pages/mine/avatar/index')
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			handleHelp() {
				this.$tab.navigateTo('/pages/mine/help/index')
			},
			handleAbout() {
				this.$tab.navigateTo('/pages/mine/about/index')
			},
			handleFeed() {
				this.$tab.navigateTo('/pages/mine/feed/feed')
			},
			handleJiaoLiuQun() {
				this.$modal.showToast('QQ群：①133713780、②146013835')
			},
			handleBuilding() {
				this.$modal.showToast('模块建设中~')
			},
			getList() {
				listNews(this.queryParams).then(response => {
					this.items = response.rows;
					this.reads = this.items.reduce((accumulator, item) => {
						if (item.newsRead === 0) {
							return accumulator + 1; // 如果newsRead等于0，累加器加1
						}
						return accumulator; // 否则返回当前累加器的值
					}, 0); // 初始累加器的值设为0

					console.log('未读新闻数量:', this.reads);
					this.footMsgFun();
				});
			},
			footMsgFun() {
				if (this.reads > 0) {
					uni.setTabBarBadge({
						index: 3,
						text: this.reads.toString(),
					});
				} else {
					uni.removeTabBarBadge({
						index: 3,
					});
				}
			}




		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f6f7;
	}

	.mine-container {
		width: 100%;
		height: 100%;


		.header-section {
			padding: 15px 15px 45px 15px;
			background-color: rgb(114, 180, 224);
			color: white;
			height: 475rpx;

			.login-tip {
				font-size: 18px;
				margin-left: 10px;
			}

			.cu-avatar {
				border: 2px solid #eaeaea;

				.icon {
					font-size: 40px;
				}
			}

			.user-info {
				margin-left: 30px;

				.u_title {
					font-size: 18px;
					line-height: 30px;
				}
			}
		}

		.content-section {
			position: relative;
			top: -220rpx;

			.mine-actions {
				margin: 15px 15px;
				padding: 13px 0px;
				border-radius: 8px;
				background-color: rgb(101, 197, 255);
				color: white;
				width: 300rpx;

				.action-item {
					.icon {
						font-size: 28px;
					}

					.text {
						display: block;
						font-size: 13px;
						margin: 8px 0px;
					}
				}
			}
		}
	}

	.hong {
		background-color: red;
		width: 20rpx;
		height: 20rpx;
		margin-right: 30rpx;
		margin-top: 10rpx;
		border-radius: 50%;
	}
</style>